package cn.com.e2suntech.demo.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.e2suntech.demo.service.model.UserInfo;

@RestController
@RequestMapping("/api")
public class MainAjaxController {
    private static final Logger logger = LoggerFactory.getLogger(MainAjaxController.class);
    
    @RequestMapping(value = "/string")
    public  String hello() {
        return "你好！hello";
        
    }

    @RequestMapping(value = "/say/{msg}")
    public String say(@PathVariable(value = "msg") String msg) {
        return "{\"msg\":\"you say:'" + msg + "'\"}";
    }
    
    @RequestMapping(value = "/map", method = RequestMethod.GET,produces="application/json")
    public Map<String,String> getMap() {
        Map<String,String> ret=new HashMap<String,String>();
        ret.put("id", "12345");
        ret.put("name", "name");
        return ret;
    }

    @RequestMapping(value = "/userinfo/{id:\\d+}", method = RequestMethod.GET,produces="application/json")
    public UserInfo getUserInfo(@PathVariable("id") Long id) {
        logger.info("获取人员信息id=" + id);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserInfoId(id);
        userInfo.setUserCode("test");
        userInfo.setUserName("张三");
        userInfo.setUserSex("男");
        return userInfo;
    }

    
    @RequestMapping(value = "/userinfo", method = RequestMethod.GET)
    public List<UserInfo> listUserInfo(@RequestParam(value = "name", required = false, defaultValue = "") String name) {

        logger.info("查询人员name like " + name);
        List<UserInfo> lstPersons = new ArrayList<UserInfo>();

        UserInfo person = new UserInfo();
        person.setUserCode("test1");
        person.setUserName("张三");
        person.setUserSex("男");
        lstPersons.add(person);

        UserInfo person2 = new UserInfo();
        person2.setUserCode("test2");
        person2.setUserName("李四");
        person2.setUserSex("男");
        lstPersons.add(person2);

        UserInfo person3 = new UserInfo();
        person3.setUserCode("test2");
        person3.setUserName("王五");
        person3.setUserSex("男");
        lstPersons.add(person3);

        return lstPersons;
    }
}
